package com.yunkeji.api.moments.mapper;

import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunkeji.api.moments.bo.MomentsListBo;
import com.yunkeji.api.moments.bo.ReplyListBo;
import com.yunkeji.api.moments.vo.MomentsListVo;
import com.yunkeji.api.moments.vo.ReplyVo;
import com.yunkeji.dao.mapper.MomentsMapper;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;
import java.util.Map;

/** @author 老张 */
public interface ApiMomentsMapper extends MomentsMapper {

    /** @author 老张 */
    @Select(
            "<script>"
                    + "SELECT m.*,"
                    + " ext.nick_name,user.online_status,"
                    + "user.sex,ext.head_url,"
                    //  关注返回1,未关注返回0
                    + "IF( ISNULL(f.sort_id),0,1) as focusFlag, "
                    //  点赞返回1,未关注返回0
                    + "IF( ISNULL(likes.from_id),0,1) as likeFlag, "
                    + "user.level,user.video_auth "
                    + " FROM ykj_moments m "
                    + "left join ykj_app_user user on m.user_id=user.id "
                    + "left join ykj_app_user_ext ext on ext.id=user.id "
                    + " LEFT JOIN ykj_moments_like likes ON likes.moments_id=m.id "
                    // 查看是否点赞
                    + " and likes.from_id=#{ownerUid} "
                    // 关联关注表
                    + "left join ykj_user_focus f ON f.focus_user_id = m.user_id"
                    // 先关联再筛选-可以携带这个列表是否被自己关注
                    + " AND f.fans_user_id =#{ownerUid} and f.del_flag=0"
                    //        未提交(0),
                    //        待审核(1),
                    //        审核通过(2),
                    //        审核拒绝(3);
                    + " where m.status=2 "
                    // 筛选-关注 f.fans_user_id 有值的都属于关注过
                    + "<if test='bo.type==2'>"
                    + "  and f.fans_user_id is not null and f.del_flag=0"
                    + "</if>"
                    // 筛选-指定某人
                    + "<if test='bo.type==3'>"
                    + "  and m.user_id=#{bo.uid} "
                    + "</if>"
                    // 筛选-查询自己
                    + "<if test='bo.type==4'>"
                    + "  and m.user_id=#{ownerUid} "
                    + "</if>"
                    + " order by create_time desc"
                    + "</script>")
    @Result(
            property = "pics",
            column = "pics",
            jdbcType = JdbcType.BLOB,
            typeHandler = JacksonTypeHandler.class)
    Page<MomentsListVo> getDefaultMoments(
            @Param("page") Page<MomentsListVo> page,
            @Param("bo") MomentsListBo build,
            @Param("ownerUid") Long ownerUid);

    /**
     * 增加动态点赞数量
     *
     * @author 老张
     * @param id
     */
    @Update("UPDATE ykj_moments SET like_num = like_num+1 WHERE id =#{id}")
    void addLikes(@Param("id") Long id);

    /**
     * 减少动态点赞数量
     *
     * @author 老张
     * @param id
     */
    @Update("UPDATE ykj_moments SET like_num = like_num-1 WHERE id =#{id}")
    void subLikes(@Param("id") Long id);

    /**
     * 增加回复点赞数量
     *
     * @author 老张
     * @param id
     */
    @Update("UPDATE ykj_moments SET reply_num = like_num+1 WHERE id =#{id}")
    void addReply(@Param("id") Long id);

    /**
     * 减少回复点赞数量
     *
     * @author 老张
     * @param id
     */
    @Update("UPDATE ykj_moments SET reply_num = like_num-1 WHERE id =#{id")
    void subReply(@Param("id") Long id);

    /**
     * 回复列表
     *
     * @author 老张
     * @param bo
     * @param userId
     * @return
     */
    @Select(
            "<script> "
                    + "SELECT "
                    + " reply.id,"
                    + " reply.user_id,"
                    + " reply.create_time, "
                    + " reply.content, "
                    + " ext.nick_name pubName, "
                    + " ext.head_url  pubHeadUrl ,moments.user_id pubUid "
                    + "FROM "
                    + " ykj_moments_reply reply "
                    + " JOIN ykj_moments moments ON moments.id = reply.moments_id  "
                    // 指定动态id
                    + " and reply.moments_id=#{bo.id} "
                    //  等于0说明是一级评论
                    + " and reply.reply_id = 0 "
                    // 如果当前查询的动态回复不是是自己，不加这个条件就是查所有
                    + "<if test='ownerId!=currentUid'>"
                    + " AND ( reply.user_id =#{currentUid} OR reply.user_id = #{ownerId} ) "
                    + "</if>"
                    + " LEFT JOIN ykj_app_user users ON reply.user_id = users.id "
                    + " LEFT JOIN ykj_app_user_ext ext ON ext.id = users.id  "
                    + " ORDER BY "
                    + " reply.create_time DESC"
                    + "</script>")
    @Results({
        @Result(property = "id", column = "id"),
        @Result(
                property = "subReplay",
                column = "{id=id,ownerId=pubUid,queryUid=user_id}",
                many = @Many(select = "findSubReply"))
    })
    Page<ReplyVo> replyList(
            @Param("page") Page<Object> page,
            @Param("bo") ReplyListBo bo,
            // 当前登录用户id
            @Param("currentUid") Long userId,
            // 动态发布者的id
            @Param("ownerId") Long ownerId);

    @Select(
            "SELECT "
                    + " reply.id,"
                    + " reply.create_time, "
                    + " reply.content, "
                    + " ext.nick_name pubName, "
                    + " ext.head_url  pubHeadUrl,ext.id userId"
                    + " FROM "
                    + " ykj_moments_reply reply "
                    + "  JOIN ykj_app_user users ON reply.user_id = users.id and reply.reply_id=#{id} "
                    + " and (reply.user_id=#{ownerId} or reply.user_id=#{queryUid})"
                    + "  JOIN ykj_app_user_ext ext ON ext.id = users.id "
                    + " ORDER BY "
                    + " reply.create_time DESC limit 2")
    /**
     * 子查询
     *
     * @author 老张
     */
    List<ReplyVo> findSubReply(@Param("param") Map<String, Object> param);

    /**
     * 回复列表
     *
     * @author 老张
     * @param bo
     * @return
     */
    @Select(
            "<script> "
                    + "SELECT "
                    + " reply.id, "
                    + " reply.user_id, "
                    + " reply.create_time, "
                    + " reply.content, "
                    + " ext.nick_name pubName, "
                    + " ext.head_url  pubHeadUrl "
                    + "FROM "
                    + " ykj_moments_reply reply "
                    + " JOIN ykj_moments moments ON moments.id = reply.moments_id  "
                    // 指定动态id
                    + "and reply.moments_id=#{momentsId} "
                    // 指定二级回复id
                    + "and reply.reply_id=#{replyId} "
                    // 如果当前查询的动态回复不是是自己，不加这个条件就是查所有
                    + "<if test='ownerId!=currentUid'>"
                    + " AND ( reply.user_id =#{currentUid} OR reply.user_id = #{ownerId} ) "
                    + "</if>"
                    + " LEFT JOIN ykj_app_user users ON reply.user_id = users.id "
                    + " LEFT JOIN ykj_app_user_ext ext ON ext.id = users.id  "
                    + "ORDER BY "
                    + " reply.create_time DESC"
                    + "</script>")
    Page<ReplyVo> replyDetailList(
            @Param("page") Page<ReplyVo> page,
            // 动态Id
            @Param("momentsId") Long bo,
            // 回复Id
            @Param("replyId") Long replyId,
            // 动态发布者的id
            @Param("ownerId") Long ownerId,
            // 当前登录用户id
            @Param("currentUid") Long userId);
}
